## 1. Granger Tests: state media concern and state public concern
rm(list=ls())
cat("\014")
wd <- "C:/data/"
setwd(wd)
load("news_data.Rdata")
require(MSBVAR)
num.paper <- 24

for (i in 1:num.paper)
{
  if(news.data[[i]]$fips.code != 99)
  {
    gr.input <- cbind(as.data.frame(news.data[[i]]$additive.ind.common), as.data.frame(news.data[[i]]$state.pub.concern.common))
    for (month.gap in c(1)){
      gr.result <- granger.test(gr.input, month.gap) 
      cat(sprintf("media->public/public->media: %-28s\t%6.3f (%.4f)\t%6.3f (%.4f)\n",news.data[[i]]$paper.name, gr.result[1,1], gr.result[1,2], gr.result[2,1], gr.result[2,2])) # national ai -> national senti sub
    }
  }
}

## 2. Granger Tests: national media concern and national subgroups public concern 
# nat.news.src.indx <- 11 #NYT
# nat.news.src.indx <- 24 #Washington Post
# nat.news.src.indx <- 7 #ABC News
# nat.news.src.indx <- 8 #CBS News
# subg.idx <- 3 #All
# subg.idx <- 4 #Edu low
# subg.idx <- 5 #Edu Mid
# subg.idx <- 6 #Edu Hig
# subg.idx <- 7 #Income high
# subg.idx <- 8 #Income low

require(MSBVAR)
load("public_concern.RData")
subg.list <-c("","","All","Low Education","Mid Education","High Education","High Income","Low Income")
yrmn.common = NULL
add.ind = NULL
pub.concern.subg = NULL
nat.news.src.indx <- 23 #NYT
subg.idx <- 3

# find overlapping months  
for(x in 1:length(public.concern$yearmnth)){
  for(y in 1:length(news.data[[nat.news.src.indx]]$yrmn.addIdx)){
    if(public.concern$yearmnth[x] == news.data[[nat.news.src.indx]]$yrmn.addIdx[y]){
      yrmn.common <- append(yrmn.common, news.data[[nat.news.src.indx]]$yrmn.addIdx[y])
      add.ind <- append(add.ind, news.data[[nat.news.src.indx]]$addIdx[y])   
      pub.concern.subg <- append(pub.concern.subg, public.concern[x,subg.idx])   
    }
  }
}

# ordering time
permu.yrmn <- order(yrmn.common)
add.ind <- add.ind[permu.yrmn]
pub.concern.subg <- pub.concern.subg[permu.yrmn]
yrmn.common <- yrmn.common[permu.yrmn]
num.dat.point <- length(yrmn.common)

# Granger test
gr.input <- as.data.frame(cbind(pub.concern.subg, add.ind))
for (month.gap in c(1,2,3)){
  gr.result <- granger.test(gr.input, month.gap) #first row: ai -> sentiment/ second: sentiment -> ai
  cat(sprintf("media->public/public->media: %-20s%-10s%3d%6.3f (%.4f)\t%6.3f (%.4f)%5d\n",news.data[[nat.news.src.indx]]$paper.name,subg.list[subg.idx], month.gap, gr.result[1,1], gr.result[1,2], gr.result[2,1], gr.result[2,2], num.dat.point)) # national ai -> national senti sub
}
